//
//  XGTopView.m
//  新闻
//
//  Created by 小果 on 16/3/30.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "XGTopView.h"
#import "NCTopStatusButton.h"

@interface XGTopView ()

{
    UIScrollView        *_scrollView;       // 滚动视图
    UIView              *_actionView;       // 操作视图
    
    UILabel             *_tipLabel;         // 提示标签
    UIButton            *_actionButton;     // 操作按钮
}
@end

@implementation XGTopView

/**
 构成：
 
 1. 状态切换按钮           负责在阅读和编辑模式来回切换
 2. 分类内容的滚动视图      _scrollView，存放已经选中的分类按钮
 3. 编辑状态下的视图        _actionView，存放一个标签和一个按钮
 */
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = kNCToolBarBGColor;
        
        // 0. 计算操作视图大小
        CGRect rect = CGRectMake(kNCSubViewsInset,
                                 0,
                                 frame.size.width - kNCSubViewsInset - kNCStatusButtonSize,
                                 frame.size.height);
        
        // 1. 创建滚动视图
        _scrollView = [[UIScrollView alloc] initWithFrame:rect];
        [self addSubview:_scrollView];
        
        // 2. 创建操作视图
        _actionView = [[UIView alloc] initWithFrame:rect];
        
        // 1> 提示标签
        _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(kNCSubViewsInset, 0, 100, rect.size.height)];
        _tipLabel.text = @"切换栏目";
        _tipLabel.font = kNCNormalFont;
        _tipLabel.textColor = [UIColor darkGrayColor];
        [_actionView addSubview:_tipLabel];
        
        // 2> 操作按钮
        NSDictionary *dict = @{NSFontAttributeName : kNCNormalFont};
        CGRect textRect = [@"排序删除" boundingRectWithSize:CGSizeMake(1000.0, 1.0)
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:dict
                                                context:nil];
        _actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _actionButton.frame = CGRectMake(rect.size.width - kNCSubViewsInset - textRect.size.width,
                                         0,
                                         textRect.size.width,
                                         rect.size.height);
        [_actionButton setTitle:@"排序删除" forState:UIControlStateNormal];
        [_actionButton setTitleColor:kNCHighlightColor forState:UIControlStateNormal];
        _actionButton.titleLabel.font = kNCNormalFont;
        
        [_actionButton addTarget:self action:@selector(changeEditMode) forControlEvents:UIControlEventTouchUpInside];
        
        [_actionView addSubview:_actionButton];
        
        // 默认编辑模式是删除条目
        self.editMode = kNCEditModeRemoveItem;
        
        [self addSubview:_actionView];
        
        _actionView.alpha = 0.0f;
        
        // 3. 创建状态按钮
        CGRect buttonFrame = CGRectMake(frame.size.width - kNCStatusButtonSize,
                                        0,
                                        kNCStatusButtonSize,
                                        kNCStatusButtonSize);
        NCTopStatusButton *button = [[NCTopStatusButton alloc] initWithFrame:buttonFrame];
        [self addSubview:button];
        
        [button addTarget:self action:@selector(clickStatusButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return self;
}

- (void)setEditMode:(kNCEditMode)editMode
{
    _editMode = editMode;
    
    if (_editMode) {
        _tipLabel.text = @"拖动排序";
        [_actionButton setTitle:@"完成" forState:UIControlStateNormal];
    } else {
        _tipLabel.text = @"切换栏目";
        [_actionButton setTitle:@"排序删除" forState:UIControlStateNormal];
    }
}

- (void)clickStatusButton:(NCTopStatusButton *)button
{
    button.isExpanded = !button.isExpanded;
    
    [UIView animateWithDuration:0.25 animations:^{
        if (button.isExpanded) {
            _scrollView.alpha = 0.0f;
            _actionView.alpha = 1.0f;
        } else {
            _scrollView.alpha = 1.0f;
            _actionView.alpha = 0.0f;
        }
    } completion:^(BOOL finished) {
        self.editMode = NO;
        
        [_delegate topViewExpandChanged:button.isExpanded];
    }];
}

- (void)changeEditMode
{
    self.editMode = !_editMode;
    
    [_delegate topViewEditModeChange:_editMode];
}

@end
